﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;

namespace CheckersClientApplication.Utility
{
    class PieceMovementValidator
    {
        private Position startPiecePosition;
        private Position endPiecePosition;
        private Position GridStartPosition;
        //private Position GridEndPosition;

        private System.Windows.Controls.UIElementCollection stackPanelList;

        private int pieceType;
        private bool IsFound;
        private bool isKing;
        private string pieceRemovedName;

        public PieceMovementValidator() 
        {
            stackPanelList = null;
            startPiecePosition = null;
            endPiecePosition = null;
            pieceType = -1;
            pieceRemovedName = "";
        }


        public string getRemovedPieceName() 
        {
            if(pieceRemovedName != null && pieceRemovedName != "")
            {
                return pieceRemovedName;    
            }

            return "";
        }

        private bool checkUp()
        {
            if ((startPiecePosition.XPos + 1 == endPiecePosition.XPos && startPiecePosition.YPos - 1 == endPiecePosition.YPos)
                || (startPiecePosition.XPos - 1 == endPiecePosition.XPos && startPiecePosition.YPos - 1 == endPiecePosition.YPos))
            {
                return true;
            }
            else
            {
                if ((startPiecePosition.XPos + 2 == endPiecePosition.XPos
                        && startPiecePosition.YPos - 2 == endPiecePosition.YPos))
                {

                    if ((isThereAPanelWithChildrenAt(startPiecePosition.XPos + 1, startPiecePosition.YPos - 1,
                                                        GridStartPosition.XPos, GridStartPosition.YPos)))
                    {

                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }



                if ((startPiecePosition.XPos - 2 == endPiecePosition.XPos
                        && startPiecePosition.YPos - 2 == endPiecePosition.YPos))
                {
                    if ((isThereAPanelWithChildrenAt(startPiecePosition.XPos - 1, startPiecePosition.YPos - 1,
                                                        GridStartPosition.XPos, GridStartPosition.YPos)))
                    {

                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }


                return false;

            }

            return false;
        }

        private bool checkDown()
        {
            

            if ((startPiecePosition.XPos + 1 == endPiecePosition.XPos && startPiecePosition.YPos + 1 == endPiecePosition.YPos) 
                || (startPiecePosition.XPos - 1 == endPiecePosition.XPos && startPiecePosition.YPos + 1 == endPiecePosition.YPos))
            {
                return true;
            }
            else
            {
                if ((startPiecePosition.XPos + 2 == endPiecePosition.XPos
                        && startPiecePosition.YPos + 2 == endPiecePosition.YPos))
                {

                    if ((isThereAPanelWithChildrenAt(startPiecePosition.XPos + 1, startPiecePosition.YPos + 1,
                                                        GridStartPosition.XPos, GridStartPosition.YPos)))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                


                if ((startPiecePosition.XPos - 2 == endPiecePosition.XPos
                        && startPiecePosition.YPos + 2 == endPiecePosition.YPos))
                {
                    if ((isThereAPanelWithChildrenAt(startPiecePosition.XPos - 1, startPiecePosition.YPos + 1,
                                                        GridStartPosition.XPos, GridStartPosition.YPos)))
                    {

                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
               

                return false;

            }

            return false;
        }

        private bool isThereAPanelWithChildrenAt(int x, int y, int ref_x, int ref_y)
        {
            
            int counter = 0;
            StackPanel panel = null;
            int pos = (x - ref_x)  + (y - ref_y) * 8;

            
            //foreach (UIElement element in this.stackPanelList)
            //{

            //    if(counter == pos)
            //    {
            //        panel = (StackPanel)element;
            //        break;
            //    }

            //    counter++;
            //}

            panel = (StackPanel)this.stackPanelList[pos];

            if (panel != null) 
            {
                if (panel.Children.Count != 0)
                {
                    Image img = null;

                    foreach(UIElement element in panel.Children)
                    {
                        if(element.GetType() == typeof(Image))
                        {
                            img = (Image)element;
                            break;
                        }
                    }

                    if (img != null) 
                    {
                        int currentPieceType = whichTypePiece(img.Name);

                        if (currentPieceType != pieceType)
                        {
                            pieceRemovedName = img.Name;
                            panel.Children.Remove(img);
                            return true;
                        }
                    }
                    

                }
                else
                {
                    return false;
                }
            }

            return false;
           
        }


        private int whichTypePiece(string pieceName)
        {
            string[] splitStrArr = pieceName.Split('_');

            if (splitStrArr[1] == "0")
            {
                return 0;
            }
            else if (splitStrArr[1] == "1")
            {
                return 1;
            }

            throw new Exception("Invalid Piece Type");

        }

        internal bool isValidMove(Position startPosition, Position endPosition, Position gridStart, int pieceType, bool isKing, System.Windows.Controls.UIElementCollection uIElementCollection)
        {
            this.startPiecePosition = startPosition;
            this.endPiecePosition = endPosition;
            this.stackPanelList = uIElementCollection;
            this.GridStartPosition = gridStart;
            this.pieceType = pieceType;

            this.isKing = isKing;

            if (isKing)
            {
                if (checkUp() )
                {
                   return true;
                }

                if(checkDown())
                {
                   return  true;
                }
            }
            if (pieceType == 0)
            {
                IsFound = checkDown();
            }
            else if (pieceType == 1)
            {
                IsFound = checkUp();
            }

            return IsFound;
        }
    }
}
